import Types from '../types';
import DataStore, {FLAG_STORAGE} from '../../expand/dao/DataStore';
import {handleData, _projectModels} from '../ActionUtil';
import FavoriteDao from '../../expand/dao/FavoriteDao';
import ProjectModel from '../../model/ProjectModel';

/**
 * 获取最热数据的异步action
 * @param flage 标识
 * @param isShowLoding 是否显示loading
 */
export function onLoadFavoriteData(flage, isShowLoding) {
  return dispatch => {
    if (isShowLoding) {
      dispatch({type: Types.FAVORITE_LOAD_DATA, storeName: flage});
    }
    new FavoriteDao(flage)
      .getAllItems()
      .then(items => {
        let resultData = [];
        for (let i = 0, len = items.length; i < len; i++) {
          resultData.push(new ProjectModel(items[i], true));
        }
        dispatch({
          type: Types.FAVORITE_LOAD_SUCCESS,
          projectModels: resultData,
          storeName: flage,
        });
      })
      .catch(e => {
        console.log(e);
        dispatch({
          type: Types.FAVORITE_LOAD_FAI,
          error: e,
          storeName: flage,
        });
      });
  };
}
